<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="css/sheep.css" />
    <script type="text/javascript" src="js/jquery-2.2.0.js" ></script>
    <script type="text/javascript" src="js/main.js" ></script>
    <script type="text/javascript" src="js/resources.js" ></script>
    <script type="text/javascript" src="js/grass.js" ></script>
    <script type="text/javascript" src="js/goods.js" ></script>
    <script type="text/javascript" src="js/box.js" ></script>
    <script type="text/javascript" src="js/button.js" ></script>
    <script type="text/javascript" src="js/star.js" ></script>
    <script type="text/javascript" src="js/level.js" ></script>
    <script type="text/javascript" src="js/toast.js" ></script>
    <script src="http://res.wx.qq.com/open/js/jweixin-1.4.0.js" type="text/javascript"></script>
    

    <title>羊了个羊</title>


</head>
<body>


    <div class="div">
        <canvas id="canvas" width="460px" height="800px"></canvas>


<audio controls autoplay hidden="True" loop id="bgmusic">
 <source src="music/bg.mp3" type="audio/mpeg" >
您的浏览器不支持 audio 元素。
</audio>
    </div>

<div id="wenzi" style="text-align:center;">
    仿羊了个羊web h5版本正式上线，有疑问加微信fbao9394
    <br>
    <a href="https://gitee.com/fangxiansheng123">源码下载地址</a>
</div>


</body>


<script type="text/javascript">




/**
 * @author ccbbs
 * @file 解决安卓webview自动播放背景音乐的问题
 */
function BGMAutoPlayMgr/* solveAndroidBGMAutoplay */(url) {
    this.audioContext = new (window.AudioContext || window.webkitAudioContext || window.mozAudioContext)();
    this.sourceNode = null;
    this.buffer = null;
    this.isPlayingBGM = false;
    this.toggleBGM = function () {
        if (typeof this.sourceNode == 'object') {
            if (this.isPlayingBGM) {
                this.sourceNode.stop();
                this.isPlayingBGM = false;
            } else this._playSourceNode();
        }
    }
    this._playSourceNode = function () {
        const audioContext = this.audioContext;
        audioContext.resume();
        const _sourceNode = audioContext.createBufferSource();
        _sourceNode.buffer = this.buffer;
        _sourceNode.loop = true;
        _sourceNode.connect(audioContext.destination);
        _sourceNode.start(0);
        this.sourceNode = _sourceNode;
        this.isPlayingBGM = true;
    }
    let loadAndAutoPlay = (audioUrl) => {
        const audioContext = this.audioContext;
        const xhr = new XMLHttpRequest();
        xhr.open('GET', audioUrl, true);
        xhr.responseType = 'arraybuffer';
        xhr.onreadystatechange = () => {
            if (xhr.status < 400 && xhr.status >= 200 && xhr.readyState === 4) {
                audioContext.decodeAudioData(xhr.response, buffer => {
                    this.buffer = buffer;
                    WeixinJSBridge.invoke("getNetworkType", {}, () => this._playSourceNode());
                });
            }
        }
        xhr.send();
    }
    loadAndAutoPlay(url);
    loadAndAutoPlay = null;
}


const bgm = new BGMAutoPlayMgr('music/bg.mp3');
function toggleBGM() {
    bgm.toggleBGM();
} 


</script>
</html>


